<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item label="分类">
				<!-- <el-select v-model="dataForm.bannerType" placeholder="分类" clearable>
					<el-option label="全部" value="" ></el-option>
					<el-option label="首页顶部banner图" value="index1" ></el-option>
					<el-option label="今日必买" value="best" ></el-option>
          <el-option label="新品上架" value="new" ></el-option>
					<el-option label="首页红包" value="reaPacket" ></el-option>
          <el-option label="中部广告位" value="middleAdver" ></el-option>
					<el-option label="超级玩家" value="player" ></el-option>
				</el-select> -->
        <el-select v-model="dataForm.bannerType" placeholder="分类" clearable>
					<el-option label="全部" value="" ></el-option>
					<el-option label="首页Banner" value="0" ></el-option>
					<el-option label="首页今日必买" value="1" ></el-option>
          <el-option label="首页新品上架" value="2" ></el-option>
					<el-option label="超级大玩家页Banner" value="3" ></el-option>
          <!-- <el-option label="首页分类活动图" value="4" ></el-option> -->
          <el-option label="社区团购" value="7" ></el-option>
          <el-option label="邀请背景图" value="8"></el-option>
          <el-option label="启动页" value="11" ></el-option>
          <el-option label="广告页" value="12"></el-option>
          <el-option label="引导页" value="13" ></el-option>
          <el-option label="商品缺省图" value="14"></el-option>
          <el-option label="购物车缺省图" value="15" ></el-option>
          <!-- <el-option label="启动广告页" value="16"></el-option> -->
          <el-option label="精品专题" value="18" ></el-option>
          <el-option label="品牌墙" value="19"></el-option>
          <el-option label="首页特卖" value="22"></el-option>
          <el-option label="首页精选" value="23"></el-option>
          <el-option label="个人中心轮播图" value="24" ></el-option>
				</el-select>
			</el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('banner:addBanner')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('banner:removeBanner')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="addTime"
        header-align="center"
        align="center"
        label="更新时间">
      </el-table-column>
      <el-table-column
        prop="bannerType"
        header-align="center"
        align="center"
        label="分类">
        <template slot-scope="scope">
          <span v-if="scope.row.bannerType == '0'">首页Banner</span>
          <span v-if="scope.row.bannerType == '1'">首页今日必买</span>
          <span v-if="scope.row.bannerType == '2'">首页新品上架</span>
          <span v-if="scope.row.bannerType == '3'">超级大玩家页Banner</span>
          <span v-if="scope.row.bannerType == '4'">首页分类活动图</span>
          <span v-if="scope.row.bannerType == '7'">社区团购</span>
          <span v-if="scope.row.bannerType == '8'">邀请背景图</span>
          <span v-if="scope.row.bannerType == '11'">启动页</span>
          <span v-if="scope.row.bannerType == '12'">广告页</span>
          <span v-if="scope.row.bannerType == '13'">引导页</span>
          <span v-if="scope.row.bannerType == '14'">商品缺省图</span>
          <span v-if="scope.row.bannerType == '15'">购物车缺省图</span>
          <span v-if="scope.row.bannerType == '16'">启动广告页</span>
          <span v-if="scope.row.bannerType == '18'">精品专题</span>
          <span v-if="scope.row.bannerType == '19'">品牌墙</span>
          <span v-if="scope.row.bannerType == '22'">首页特卖</span>
          <span v-if="scope.row.bannerType == '23'">首页精选</span>
          <span v-if="scope.row.bannerType == '24'">个人中心轮播图</span>
          <span v-if="scope.row.bannerType == 'index1'">首页Banner</span>
          <span v-if="scope.row.bannerType == 'best'">首页今日必买</span>
          <span v-if="scope.row.bannerType == 'new'">首页新品上架</span>
          <span v-if="scope.row.bannerType == 'reaPacket'">首页红包</span>
          <span v-if="scope.row.bannerType == 'player'">超级大玩家页Banner</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="appType"
        header-align="center"
        align="center"
        label="平台">
        <template slot-scope="scope">
          <span v-if="scope.row.appType === 0">全部</span>
          <span v-if="scope.row.appType === 1">安卓</span>
          <span v-if="scope.row.appType === 2">IOS</span>
          <span v-if="scope.row.appType === 3">小程序</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="imgName"
        header-align="center"
        align="center"
        label="图片">
        <template slot-scope="scope">
          <el-popover
          placement="right"
          title=""
          trigger="hover">
          <img :src="'http://image.yilianbaihui.cn/consumer/' + scope.row.imgPath + '/' + scope.row.imgName" style="max-height: 160px;max-width: 200px">
          <img slot="reference" :src="'http://image.yilianbaihui.cn/consumer/' + scope.row.imgPath + '/' + scope.row.imgName" style="max-height: 50px;max-width: 80px">
        </el-popover>
        </template>
      </el-table-column>
      <el-table-column
        prop="showStatus"
        header-align="center"
        align="center"
        label="显示状态">
        <template slot-scope="scope">
          <el-switch
            style="margin-left: 10px;margin-bottom: 5px;"
            v-model="scope.row.showStatus"
            :active-value= 1
            :inactive-value= 0
            @change="batch(scope.row.id, scope.row.showStatus)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        prop="addTime"
        header-align="center"
        align="center"
        label="时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './apphomebanner-add-or-update'
  export default {
    data () {
      return {
        dataForm: {
          bannerType: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/banner/queryBannerList'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'bannerType': this.dataForm.bannerType
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.data.list
            this.totalPage = data.data.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定进行删除操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/banner/removeBanner'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      batch (id, showStatus) {
        this.$confirm(`确定对该banner进行显示操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl(`/banner/uploadBanner/${id}/${showStatus}`),
            method: 'post',
            data: this.$http.adornData()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
